package com.shop.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.shop.mapper.AdminMapper;
import com.shop.mapper.CategoryMapper;
import com.shop.mapper.GoodsMapper;
import com.shop.model.Category;
import com.shop.model.Goods;

import com.shop.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    public List<Category> selectAllCategory() {
        return categoryMapper.selectAllCategory();
    }

    @Override
    public Boolean categoryAdd(String name, Integer pid) {
         Category category=categoryMapper.selectByName(name);
         if(category==null){
             categoryMapper.CategoryAdd(name, pid);
             return false;
         }
         return true;
    }

    @Override
    public Boolean categoryUpdate(Map map) {
        ObjectMapper mapper = new ObjectMapper();
        Category category= mapper.convertValue(map, Category.class);
        Category categoryTemp=categoryMapper.selectByName(category.getName());
        if(categoryTemp==null||categoryTemp.getId()==category.getId()){
            categoryMapper.update(category);
            return true;
        }
        return false;
    }

    @Override
    public Boolean delete(Integer id) {
        List<Goods> goods=categoryMapper.selectGoodsByCategoryId(id);
        List<Integer> category=categoryMapper.selectCategoryIdByPid(id);
        if(goods!=null||category!=null) return false;
        categoryMapper.delete(id);
        return true;
    }

    @Override
    public List<Integer> selectCategoryIdByPid(int categoryId) {
        return categoryMapper.selectCategoryIdByPid(categoryId);
    }


}
